package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.dto.R;
import com.qf.ssm.pojo.User;
import com.qf.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //添加用户
    @PostMapping("/user")
    public R addUser(@RequestBody User user){
        userService.addUser(user);
        return R.setOk();
    }

    @GetMapping("/user/{id}")
    public R findUserById(@PathVariable Long id){
        User user = userService.findUserById(id);
        return R.setOk(user);

    }

    @GetMapping("/users")
    public R findAllUserByPage(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "2") int pageSize){
        PageInfo<User> pageInfo = userService.findUserByPage(pageNum,pageSize);
        return R.setOk(pageInfo);

    }
}
